home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / magict_1 / simplewe.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-07-22  |  16.0 KB  |  602 lines

  1. VERSION 5.00
  2. Object = "{3035B5D2-295D-11D3-8C54-006008BA8D16}#1.0#0"; "MAGICTCP.OCX"
  3. Begin VB.Form Form1 
  4.    Caption         =   "Form1"
  5.    ClientHeight    =   8010
  6.    ClientLeft      =   60
  7.    ClientTop       =   345
  8.    ClientWidth     =   10080
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   8010
  11.    ScaleWidth      =   10080
  12.    StartUpPosition =   3  'Windows-Standard
  13.    Begin VB.CheckBox chkSubdirs 
  14.       Caption         =   "Enable Subdirectories"
  15.       Height          =   255
  16.       Left            =   8040
  17.       TabIndex        =   25
  18.       Top             =   240
  19.       Width           =   1935
  20.    End
  21.    Begin VB.CommandButton cmdSave 
  22.       Caption         =   "Save Config"
  23.       Height          =   495
  24.       Left            =   8040
  25.       TabIndex        =   24
  26.       Top             =   1200
  27.       Width           =   1455
  28.    End
  29.    Begin VB.TextBox txtLog 
  30.       Height          =   2655
  31.       Left            =   240
  32.       MultiLine       =   -1  'True
  33.       ScrollBars      =   2  'Vertikal
  34.       TabIndex        =   22
  35.       TabStop         =   0   'False
  36.       Top             =   5280
  37.       Width           =   9735
  38.    End
  39.    Begin VB.TextBox txtPhysical 
  40.       Height          =   405
  41.       Index           =   7
  42.       Left            =   3960
  43.       TabIndex        =   21
  44.       Top             =   4560
  45.       Width           =   3615
  46.    End
  47.    Begin VB.TextBox txtVirtual 
  48.       Height          =   405
  49.       Index           =   7
  50.       Left            =   240
  51.       TabIndex        =   20
  52.       Top             =   4560
  53.       Width           =   3615
  54.    End
  55.    Begin VB.TextBox txtPhysical 
  56.       Height          =   405
  57.       Index           =   6
  58.       Left            =   3960
  59.       TabIndex        =   19
  60.       Top             =   4080
  61.       Width           =   3615
  62.    End
  63.    Begin VB.TextBox txtVirtual 
  64.       Height          =   405
  65.       Index           =   6
  66.       Left            =   240
  67.       TabIndex        =   18
  68.       Top             =   4080
  69.       Width           =   3615
  70.    End
  71.    Begin VB.TextBox txtPhysical 
  72.       Height          =   405
  73.       Index           =   5
  74.       Left            =   3960
  75.       TabIndex        =   17
  76.       Top             =   3600
  77.       Width           =   3615
  78.    End
  79.    Begin VB.TextBox txtVirtual 
  80.       Height          =   405
  81.       Index           =   5
  82.       Left            =   240
  83.       TabIndex        =   16
  84.       Top             =   3600
  85.       Width           =   3615
  86.    End
  87.    Begin VB.TextBox txtPhysical 
  88.       Height          =   405
  89.       Index           =   4
  90.       Left            =   3960
  91.       TabIndex        =   15
  92.       Top             =   3120
  93.       Width           =   3615
  94.    End
  95.    Begin VB.TextBox txtVirtual 
  96.       Height          =   405
  97.       Index           =   4
  98.       Left            =   240
  99.       TabIndex        =   14
  100.       Top             =   3120
  101.       Width           =   3615
  102.    End
  103.    Begin VB.TextBox txtPhysical 
  104.       Height          =   405
  105.       Index           =   3
  106.       Left            =   3960
  107.       TabIndex        =   13
  108.       Top             =   2640
  109.       Width           =   3615
  110.    End
  111.    Begin VB.TextBox txtVirtual 
  112.       Height          =   405
  113.       Index           =   3
  114.       Left            =   240
  115.       TabIndex        =   9
  116.       Top             =   2640
  117.       Width           =   3615
  118.    End
  119.    Begin VB.TextBox txtPhysical 
  120.       Height          =   405
  121.       Index           =   2
  122.       Left            =   3960
  123.       TabIndex        =   8
  124.       Top             =   2160
  125.       Width           =   3615
  126.    End
  127.    Begin VB.TextBox txtVirtual 
  128.       Height          =   405
  129.       Index           =   2
  130.       Left            =   240
  131.       TabIndex        =   7
  132.       Top             =   2160
  133.       Width           =   3615
  134.    End
  135.    Begin VB.TextBox txtPhysical 
  136.       Height          =   405
  137.       Index           =   1
  138.       Left            =   3960
  139.       TabIndex        =   6
  140.       Top             =   1680
  141.       Width           =   3615
  142.    End
  143.    Begin VB.TextBox txtVirtual 
  144.       Height          =   405
  145.       Index           =   1
  146.       Left            =   240
  147.       TabIndex        =   5
  148.       Top             =   1680
  149.       Width           =   3615
  150.    End
  151.    Begin VB.TextBox txtPhysical 
  152.       Height          =   405
  153.       Index           =   0
  154.       Left            =   3960
  155.       TabIndex        =   4
  156.       ToolTipText     =   "Corresponding physical path for virtual path to the left"
  157.       Top             =   1200
  158.       Width           =   3615
  159.    End
  160.    Begin VB.TextBox txtVirtual 
  161.       Height          =   405
  162.       Index           =   0
  163.       Left            =   240
  164.       TabIndex        =   3
  165.       ToolTipText     =   "Virtual path"
  166.       Top             =   1200
  167.       Width           =   3615
  168.    End
  169.    Begin VB.CommandButton cmdStart 
  170.       Caption         =   "Start!"
  171.       Default         =   -1  'True
  172.       Height          =   495
  173.       Left            =   8040
  174.       TabIndex        =   23
  175.       Top             =   4440
  176.       Width           =   1455
  177.    End
  178.    Begin VB.TextBox txtPort 
  179.       Height          =   375
  180.       Left            =   600
  181.       TabIndex        =   0
  182.       ToolTipText     =   "TCP port where to listen"
  183.       Top             =   240
  184.       Width           =   855
  185.    End
  186.    Begin VB.TextBox txtAccessList 
  187.       Height          =   375
  188.       Left            =   2760
  189.       TabIndex        =   2
  190.       ToolTipText     =   "Comma separeted list of ip adresses containing wild card '*'"
  191.       Top             =   240
  192.       Width           =   4815
  193.    End
  194.    Begin M3LibCtl.MagicTCP M1 
  195.       Left            =   8400
  196.       OleObjectBlob   =   "SimpleWeb.frx":0000
  197.       Top             =   2520
  198.    End
  199.    Begin VB.Line Line2 
  200.       X1              =   120
  201.       X2              =   9960
  202.       Y1              =   5160
  203.       Y2              =   5160
  204.    End
  205.    Begin VB.Label Label4 
  206.       Caption         =   "Phsical Path"
  207.       Height          =   255
  208.       Left            =   3960
  209.       TabIndex        =   12
  210.       Top             =   960
  211.       Width           =   3135
  212.    End
  213.    Begin VB.Label Label3 
  214.       Caption         =   "Virtual Path"
  215.       Height          =   255
  216.       Left            =   240
  217.       TabIndex        =   11
  218.       Top             =   960
  219.       Width           =   3135
  220.    End
  221.    Begin VB.Line Line1 
  222.       X1              =   120
  223.       X2              =   9960
  224.       Y1              =   840
  225.       Y2              =   840
  226.    End
  227.    Begin VB.Label Label2 
  228.       Caption         =   "Access List"
  229.       Height          =   375
  230.       Left            =   1800
  231.       TabIndex        =   10
  232.       Top             =   240
  233.       Width           =   855
  234.    End
  235.    Begin VB.Label Label1 
  236.       Caption         =   "Port"
  237.       Height          =   255
  238.       Left            =   240
  239.       TabIndex        =   1
  240.       Top             =   240
  241.       Width           =   375
  242.    End
  243. Attribute VB_Name = "Form1"
  244. Attribute VB_GlobalNameSpace = False
  245. Attribute VB_Creatable = False
  246. Attribute VB_PredeclaredId = True
  247. Attribute VB_Exposed = False
  248. Option Explicit
  249. Const MAX_VPATHS = 8
  250. Dim ServerSocket As Long
  251. Dim ServerPort As Long
  252. Dim AccessList As String
  253. Dim serverSubdirs As Long
  254. Dim vPath(1 To 8) As String ' our virtual paths
  255. Dim pPath(1 To 8) As String ' corresponding physical paths
  256. Sub CloseSocket()
  257. With M1
  258.     If .zzState >= 0 Then
  259.         Close .zzState
  260.     End If
  261.     .Delete .CurrentSocket
  262. End With
  263. End Sub
  264. Sub HttpError(c As Integer, t As String)
  265. ' return an http error
  266. Dim s As String
  267. Dim r As String
  268. Dim n As Long
  269. With M1
  270.     r = "Error: " & t
  271.     s = "HTTP/1.0 " & CStr(c) & " " & t & vbCrLf
  272.     s = s & "Content-Type: text/plain" & vbCrLf
  273.     s = s & "Content-Length: " & CStr(Len(r)) & vbCrLf
  274.     s = s & vbCrLf
  275.     s = s & r
  276.     n = .WriteString(s)
  277.     If (n > 0) And (n < Len(s)) Then
  278.         .zzBuffer = Mid$(s, n + 1)
  279.     End If
  280. End With
  281. End Sub
  282. Function IsPostfix(s As String, p As String) As Boolean
  283. Dim tf As Boolean
  284. If Len(p) = 0 Then
  285.     tf = True
  286.     If (Len(s) = 0) Or (Len(s) < Len(p)) Then
  287.         tf = False
  288.     Else
  289.         tf = Mid$(s, Len(s) - Len(p) + 1) = p
  290.     End If
  291. End If
  292. IsPostfix = tf
  293. End Function
  294. Sub LoadConfig()
  295. ' load config from inifile
  296. Dim i As Integer
  297. Dim k As String
  298. Dim v As String
  299. Dim p As String
  300. Dim c As Integer
  301. With M1
  302.     For i = 1 To MAX_VPATHS
  303.         k = "VPATH" & CStr(i)
  304.         
  305.         v = .GetProfileString("MAPPING", k, "")
  306.         v = Trim$(v)
  307.         
  308.         If v <> "" Then
  309.             p = .GetProfileString("MAPPING", v, "")
  310.             p = Trim$(p)
  311.         End If
  312.         
  313.         If (v <> "") And (p <> "") Then
  314.             If Not IsPostfix(v, "/") Then
  315.                 v = v & "/"
  316.             End If
  317.             
  318.             If Not IsPostfix(p, "\") Then
  319.                 p = p & "\"
  320.             End If
  321.             
  322.             vPath(i) = v
  323.             pPath(i) = p
  324.             
  325.             txtVirtual(i - 1) = v
  326.             txtPhysical(i - 1) = p
  327.         End If
  328.     Next i
  329.     ServerPort = .GetProfileInt("CONFIG", "PORT", 80)
  330.     txtPort = CStr(ServerPort)
  331.     AccessList = .GetProfileString("CONFIG", "ACCESSLIST", "")
  332.     txtAccessList = AccessList
  333.     serverSubdirs = CInt(.GetProfileInt("CONFIG", "SUBDIRS", 0))
  334.     chkSubdirs = serverSubdirs
  335.     cmdSave.Enabled = False
  336. End With
  337. End Sub
  338. Function MapPath(vp As String) As String
  339. ' find physical path for a given path
  340. Dim i As Integer
  341. Dim pd As String
  342. Dim tf As Boolean
  343. i = 1
  344. tf = False
  345. pd = ""
  346. While (i < MAX_VPATHS) And Not tf
  347.     If vp = vPath(i) Then
  348.         tf = True
  349.         pd = pPath(i)
  350.     Else
  351.         i = i + 1
  352.     End If
  353. MapPath = pd
  354. End Function
  355. Function OpenFile(path As String) As Boolean
  356. ' open requested file
  357. Dim i As Integer
  358. Dim tf As Boolean
  359. Dim vp As String
  360. Dim pd As String
  361. Dim fn As String
  362. Dim ct As String
  363. Dim s As String
  364. On Error GoTo O_ERR
  365. tf = True
  366. If tf Then
  367.     i = InStrRev(path, "/")
  368.     If (i = 0) Then
  369.         tf = False
  370.     End If
  371. End If
  372. ' get the physical path and filename
  373. If tf Then
  374.     tf = False
  375.     Do
  376.         vp = Left$(path, i)
  377.         pd = MapPath(vp)
  378.         tf = Len(pd) > 0
  379.         If Not tf Then
  380.             path = Left$(path, i - 1) & "\" & Mid$(path, i + 1)
  381.             i = i - 1
  382.             If (i > 0) Then
  383.                 i = InStrRev(path, "/", i)
  384.             End If
  385.         End If
  386.     Loop While Not tf And (i > 0) And serverSubdirs
  387.     If tf Then
  388.        fn = Mid$(path, i + 1)
  389.     End If
  390. End If
  391. ' test if file can be found
  392. If tf Then
  393.     fn = pd & fn
  394.     tf = (Dir$(fn) <> "")
  395. End If
  396. If tf Then
  397.     ' get mime type
  398.     i = InStr(fn, ".")
  399.     If i > 0 Then
  400.         ct = Mid$(fn, i + 1)
  401.     Else
  402.         ct = ""
  403.     End If
  404.     Select Case LCase$(ct)
  405.         Case "htm", "html"
  406.             ct = "text/html"
  407.         
  408.         Case "jpg", "jpeg"
  409.             ct = "image/jpeg"
  410.             
  411.         Case "gif"
  412.             ct = "image/gif"
  413.             
  414.         Case Else
  415.             ct = M1.GetProfileString("CONTENT-TYPES", ct, "")
  416.             If ct = "" Then
  417.                 ct = "text/plain"
  418.             End If
  419.     End Select
  420.     ' response header
  421.     s = "HTTP/1.0 200 OK" & vbCrLf
  422.     s = s & "Content-Type: " & ct & vbCrLf
  423.     s = s & "Content-Length: " & FileLen(fn) & vbCrLf
  424.     s = s & vbCrLf
  425.     ' write header to browser
  426.     i = M1.WriteString(s)
  427.     tf = (i = Len(s))
  428. End If
  429. If tf Then
  430.     tf = M1.WriteFile(fn)
  431. End If
  432. O_EXIT:
  433.     If Not tf Then
  434.         HttpError 404, "File not found"
  435.     End If
  436.     OpenFile = tf
  437.     Exit Function
  438. O_ERR:
  439.     tf = False
  440.     MsgBox Err.Description
  441.     Resume O_EXIT
  442. End Function
  443. Sub SaveConfig()
  444. ' Save configuration to inifile
  445. Dim i As Integer
  446. Dim k As String
  447. Dim v As String
  448. Dim p As String
  449. With M1
  450.     For i = 1 To MAX_VPATHS
  451.         v = Trim$(txtVirtual(i - 1))
  452.         p = Trim$(txtPhysical(i - 1))
  453.         
  454.         If (v <> "") And (p <> "") Then
  455.             k = "VPATH" & CStr(i)
  456.             .SetProfileString "MAPPING", k, v
  457.             .SetProfileString "MAPPING", v, p
  458.         End If
  459.     Next i
  460.     .SetProfileInt "CONFIG", "PORT", CLng(txtPort)
  461.     .SetProfileString "CONFIG", "ACCESSLIST", LTrim$(RTrim$(txtAccessList))
  462.     .SetProfileInt "CONFIG", "SUBDIRS", CLng(chkSubdirs)
  463.     cmdSave.Enabled = False
  464. End With
  465. End Sub
  466. Function Split(ByRef s As String, d As String) As String
  467. Dim i As Integer
  468. i = InStr(s, d)
  469. If (i = 0) Then
  470.     Split = s
  471.     s = ""
  472.     Split = Left$(s, i - 1)
  473.     s = Mid$(s, i + Len(d))
  474. End If
  475. End Function
  476. Sub StartStop()
  477. With M1
  478.     ' Active socket?
  479.     If .IsValidSocket(ServerSocket) Then
  480.         ' stop server
  481.         .Delete ServerSocket
  482.         cmdStart.Caption = "Start!"
  483.     Else
  484.         ' create a new socket for listening
  485.         ServerSocket = .New
  486.         .CurrentSocket = ServerSocket
  487.         
  488.         ' Transfer attributes from configuration
  489.         .LocalPort = CLng(txtPort)
  490.         .ReUseAddr = True
  491.         .AccessList = AccessList
  492.         
  493.         ' start listening for incoming connection requests
  494.         If Not .Listen Then
  495.             MsgBox "Cannot start server: " & .LastErrorText
  496.             .Delete ServerSocket
  497.         Else
  498.             cmdStart.Caption = "Stop!"
  499.         End If
  500.     End If
  501. End With
  502. End Sub
  503. Private Sub chkSubdirs_Click()
  504. cmdSave.Enabled = True
  505. End Sub
  506. Private Sub cmdSave_Click()
  507. If MsgBox("Save Configuration", vbYesNo Or vbQuestion) = vbYes Then
  508.     SaveConfig
  509.     LoadConfig
  510. End If
  511. End Sub
  512. Private Sub cmdStart_Click()
  513. StartStop
  514. End Sub
  515. Private Sub Form_Load()
  516. ' No Server Socket
  517. ServerSocket = -1
  518. ' LoadConfig
  519. LoadConfig
  520. ' Start HTTP-Service
  521. StartStop
  522. End Sub
  523. Private Sub M1_OnClose()
  524. CloseSocket
  525. End Sub
  526. Private Sub M1_OnError(ByVal WinsockError As Long, ByVal Func As String)
  527. CloseSocket
  528. End Sub
  529. Private Sub M1_OnFileWritten(ByVal Success As Boolean, ByVal Filename As String)
  530. CloseSocket
  531. End Sub
  532. Private Sub M1_OnRead()
  533. Dim tf As Boolean
  534. Dim take As Boolean
  535. Dim done As Boolean
  536. Dim s As String
  537. Dim method As String
  538. Dim path As String
  539. Dim version As String
  540. Dim i As Integer
  541. With M1
  542.     take = False
  543.     done = False
  544.     ' read line from browser
  545.     tf = .ReadString(s)
  546.     If tf Then
  547.         ' merge data already read and new data
  548.         s = .zzBuffer & s
  549.         
  550.         ' request line is terminated by CR LF
  551.         i = InStr(s, vbCrLf)
  552.         take = i > 0
  553.         If Not take Then
  554.             ' line not complete, store data read so far
  555.             .zzBuffer = s
  556.         End If
  557.     End If
  558.     If tf And take Then
  559.         ' log request
  560.         s = Left$(s, i - 1)
  561.         txtLog = txtLog & .RemoteHost & ":" & .RemotePort & " " & s & vbCrLf
  562.         txtLog.SelStart = Len(txtLog) + 1
  563.         
  564.         .zzBuffer = ""
  565.         
  566.         method = Split(s, " ")
  567.         path = Split(s, " ")
  568.         version = Split(s, " ")
  569.         
  570.         ' only GET method ist accepted
  571.         If method <> "GET" Then
  572.             HttpError 400, method & " not supported"
  573.             done = True
  574.         End If
  575.     End If
  576.     If tf And take And Not done Then
  577.         ' open file for transfer
  578.         done = Not OpenFile(path)
  579.     End If
  580.     'If tf And take And Not done Then
  581.         ' start writing to browser
  582.     '    done = Not WriteFile
  583.     'End If
  584.     If done Or Not tf Then
  585.         ' close on error
  586.         CloseSocket
  587.     End If
  588. End With
  589. End Sub
  590. Private Sub txtAccessList_Change()
  591. cmdSave.Enabled = True
  592. End Sub
  593. Private Sub txtPhysical_Change(Index As Integer)
  594. cmdSave.Enabled = True
  595. End Sub
  596. Private Sub txtPort_Change()
  597. cmdSave.Enabled = True
  598. End Sub
  599. Private Sub txtVirtual_Change(Index As Integer)
  600. cmdSave.Enabled = True
  601. End Sub
  602.